<template>
  <a-modal
    title="考勤规则设置"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="入园时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <!-- <a-time-picker class="fullwidth" :default-value="moment('8:30', 'HH:mm')" format="HH:mm" /> -->
          <a-time-picker v-decorator="['entryTime', {rules: [{required: true, message: '请选择入园时间！'}]}]" format="HH:mm" valueFormat="HH:mm" />
        </a-form-item>
        <a-form-item
          label="离园时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <!-- <a-time-picker class="fullwidth" :default-value="moment('16:30', 'HH:mm')" format="HH:mm" /> -->
          <a-time-picker v-decorator="['outSchoolTime', {rules: [{required: true, message: '请选择离园时间！'}]}]" format="HH:mm"  valueFormat="HH:mm" />
        </a-form-item>

      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
  import { courseTypeEdit } from '@/api/modular/garden/courseManage'
import { getChildAttendanceRule, addChildAttendanceRule } from '@/api/modular/garden/kaoqin';
import moment from 'moment';

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true;
        getChildAttendanceRule().then(res => {
          if (!res.success) {
            return
          }
          let startt = this.moment(res.data.entryTime, 'HH:mm');
          let endt = this.moment(res.data.outSchoolTime, 'HH:mm');
          this.$nextTick(() => {
            this.form.setFieldsValue(
              {
                entryTime: startt,
                outSchoolTime: endt
              }
            )
          })
        })
      },

      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true;
        validateFields((errors, values) => {
          console.log(errors, values)
          if (!errors) {
            values['entryTime'] = moment(values.entryTime).format('HH:mm')
            values['outSchoolTime'] = moment(values.outSchoolTime).format('HH:mm')
            addChildAttendanceRule(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
